package com.um.swing;

import cn.com.antcloud.api.AntFinTechApiClient;
import cn.com.antcloud.api.AntFinTechProfile;
import cn.com.antcloud.api.appex.v1_0_0.request.GetSolutionFilenotarystatusRequest;
import cn.com.antcloud.api.appex.v1_0_0.request.InitSolutionFilenotaryRequest;
import cn.com.antcloud.api.appex.v1_0_0.request.QuerySolutionFilenotaryRequest;
import cn.com.antcloud.api.appex.v1_0_0.request.SyncSolutionFilenotaryRequest;
import cn.com.antcloud.api.appex.v1_0_0.response.GetSolutionFilenotarystatusResponse;
import cn.com.antcloud.api.appex.v1_0_0.response.InitSolutionFilenotaryResponse;
import cn.com.antcloud.api.appex.v1_0_0.response.QuerySolutionFilenotaryResponse;
import cn.com.antcloud.api.appex.v1_0_0.response.SyncSolutionFilenotaryResponse;
import com.alibaba.fastjson.JSON;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class MayiChanMain {

    public static void main(String[] args) throws InterruptedException, IOException, UnirestException {
        //0. 初始化client
        String endpoint = "https://prodapigw.cloud.alipay.com";
        String accessKey = "LTAI5tCQuDPcLUL8SVNaazHi";
        String secretKey = "lkeeOVd3SWGjYohvomwzvU3nozfsDv";
        String instanceId = "DWXPCLWT";
        String fileAppDid = "did:mychain:67205e3c20299aa4378547b937dd1594f9242082ba9aa77abb2a76f883511c99";
        AntFinTechProfile antFinTechProfile = AntFinTechProfile.getProfile(endpoint, accessKey, secretKey);
        AntFinTechApiClient antFinTechApiClient = new AntFinTechApiClient(antFinTechProfile);
        //1. init oss 地址
        InitSolutionFilenotaryRequest initSolutionFilenotaryRequest = new InitSolutionFilenotaryRequest(instanceId);
        initSolutionFilenotaryRequest.setAppDid(fileAppDid);
        InitSolutionFilenotaryResponse initSolutionFilenotaryResponse = antFinTechApiClient.execute(initSolutionFilenotaryRequest);
        System.out.println(JSON.toJSONString(initSolutionFilenotaryResponse));
        String fileNotaryId = initSolutionFilenotaryResponse.getFileNotaryId();
        //2.通过http PUT请求你上传文件
        HttpResponse<String> resp = Unirest.put(initSolutionFilenotaryResponse.getUrl())
                .header("Content-Type", null)
                .body(getBytesByFile("/Users/enizumi/Downloads/pub.txt"))
                .asString();

        if (resp.getStatus()!=200) {
            //文件上传失败,此处可添加上传失败后的处理
            return;
        }else{
            System.out.println("文件上传成功！");
        }
        //3. 通知服务端完成上传,开始存证
        SyncSolutionFilenotaryRequest syncSolutionFilenotaryRequest = new SyncSolutionFilenotaryRequest(instanceId);
        syncSolutionFilenotaryRequest.setAppDid(fileAppDid);
        syncSolutionFilenotaryRequest.setFileNotaryId(fileNotaryId);
        SyncSolutionFilenotaryResponse syncSolutionFilenotaryResponse = antFinTechApiClient.execute(syncSolutionFilenotaryRequest);
        System.out.println(JSON.toJSONString(initSolutionFilenotaryResponse));
        if (!syncSolutionFilenotaryResponse.getAccepted()) {
            //存证被拒绝了 此处可自行添加被拒绝后的处理逻辑
            return;
        }
        //4.获取存证状态
        GetSolutionFilenotarystatusRequest getSolutionFilenotarystatusRequest = new GetSolutionFilenotarystatusRequest(instanceId);
        getSolutionFilenotarystatusRequest.setAppDid(fileAppDid);
        getSolutionFilenotarystatusRequest.setFileNotaryId(fileNotaryId);
        String txHash;
        //此处轮询查询状态接口获得异步存证是否成功的结果
        //实际代码中请自行处理轮询逻辑和超时逻辑
        while (true) {
            GetSolutionFilenotarystatusResponse getSolutionFilenotarystatusResponse = antFinTechApiClient.execute(getSolutionFilenotarystatusRequest);
            if (getSolutionFilenotarystatusResponse.getFinished()) {
                txHash = getSolutionFilenotarystatusResponse.getTxHash();
                System.out.println("完成存证交易hash为："+txHash);
                break;
            }
            Thread.sleep(3 * 1000);
        }
        //4.存证查询
        QuerySolutionFilenotaryRequest querySolutionFilenotaryRequest = new QuerySolutionFilenotaryRequest(instanceId);
        querySolutionFilenotaryRequest.setAppDid(fileAppDid);
        querySolutionFilenotaryRequest.setTxHash(txHash);
        QuerySolutionFilenotaryResponse querySolutionFilenotaryResponse = antFinTechApiClient.execute(querySolutionFilenotaryRequest);
        if (querySolutionFilenotaryResponse != null) {
            //不抛错且能够获得返回值即表示查询成功
            System.out.println("存证信息如下：");
            System.out.println("url :" + querySolutionFilenotaryResponse.getUrl());
            System.out.println("qrCodeUrl :" + querySolutionFilenotaryResponse.getQrCodeUrl());
            System.out.println("fileContentHash" + querySolutionFilenotaryResponse.getFileContentHash());
        }
    }

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
